package com.dindang.maintenance.app.utils;

import com.dindang.maintenance.mvp.model.entity.Maintain;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by 70339 on 2018/5/17.
 * 代码无八阿哥
 * 阿弥陀佛
 */
public class TimeComparisonUtils {

    public static String getDate(){
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(nowDate);
        return dateString;
    }

    public static String dateTString(Date dt1){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分");
        String dateString = formatter.format(dt1);
        return dateString;
    }

    public static String dateTDateString(Date dt1) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dt1);
        return dateString;
    }

    public static String dateTTimeString(Date dt1) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        String dateString = formatter.format(dt1);
        return dateString;
    }

    public static Date StringTdate(String dt1) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateString = formatter.parse(dt1);
        return dateString;
    }

    public static String dateTString2(Date dt1){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dt1);
        return dateString;
    }

    public static String dateTString3(Date dt1){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dt1);
        return dateString;
    }

    /**
     * hour增加需要的时间
     * @param date 需要被增加的时间
     * @param addHour 需要增加多少
     * @return
     */
    public static Date hourAdd(Date date, int addHour){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR, addHour);
        return c.getTime();
    }

    public static String dateTTime(Date dt1){
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        String dateString = formatter.format(dt1);
        return dateString;
    }

    public static String dateTMDString(Date dt1){
        Date nowDate = new Date();

        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        String dateString = formatter.format(dt1);
        if(dateTString2(dt1).equals(dateTString2(nowDate))){
            return "今日";
        }
        SimpleDateFormat year = new SimpleDateFormat("yyyy");
        int contrast = selectedDate.get(Calendar.YEAR);

        if(String.valueOf(contrast - 1).equals(year.format(dt1))){
            dateString = "去年 " + dateString;
        }else if(String.valueOf(contrast).equals(year.format(dt1))){
            dateString = "今年 " + dateString;
        }else if(String.valueOf(contrast + 1).equals(year.format(dt1))){
            dateString = "明年 " + dateString;
        }
        return dateString;
    }

    public static String dateTChineseString(Date dt1){
        Date nowDate = new Date();

        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dt1);
        if(dateTString2(dt1).equals(dateTString2(nowDate))){
            return "今天";
        }
        selectedDate.add(Calendar.DATE, +1);

        if(dateTString2(selectedDate.getTime()).equals(formatter.format(dt1))){
            dateString = "明天";
        }

        return dateString;
    }

    /**
     * 分钟取整
     * @return
     */
    public static Calendar getStartTimeInteger() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart;
    }

    /**
     * 如果当前时间在00:01-12:00择返回12.00
     * 如果当前时间在12:01-23:59择返回后一天的00:00
     * @return
     */
    public static Calendar getStartTimeInteger2(){
        Calendar nowDate = Calendar.getInstance();//当前时间

        Calendar noonDate = Calendar.getInstance();//中午时间
        noonDate.set(Calendar.HOUR_OF_DAY, 12);
        noonDate.set(Calendar.MINUTE, 0);
        noonDate.set(Calendar.SECOND, 0);
        noonDate.set(Calendar.MILLISECOND, 0);

        if(nowDate.getTime().getTime() <= noonDate.getTime().getTime()){
            nowDate = noonDate;
        }else{
            nowDate.add(Calendar.DATE, 1);
            nowDate.set(Calendar.HOUR_OF_DAY, 0);
            nowDate.set(Calendar.MINUTE, 0);
            nowDate.set(Calendar.SECOND, 0);
            nowDate.set(Calendar.MILLISECOND, 0);
        }

        return nowDate;
    }

    /**
     *
     * @return
     */
    public static Maintain.SendParam getSendParam(){
        Maintain.SendParam sendParam = new Maintain.SendParam();
        Calendar nowDate = Calendar.getInstance();//当前时间

        Calendar noonDate = Calendar.getInstance();//中午时间
        noonDate.set(Calendar.HOUR_OF_DAY, 12);
        noonDate.set(Calendar.MINUTE, 0);
        noonDate.set(Calendar.SECOND, 0);
        noonDate.set(Calendar.MILLISECOND, 0);

        if(nowDate.getTime().getTime() <= noonDate.getTime().getTime()){
            nowDate = noonDate;
            sendParam.selectDate = nowDate.getTime();
            sendParam.startTime = TimeComparisonUtils.hourAdd(sendParam.selectDate, 1);
            sendParam.endTime = calendarAddHour(sendParam.startTime, 2);
        }else{
            nowDate.add(Calendar.DATE, 1);
            nowDate.set(Calendar.HOUR_OF_DAY, 0);
            nowDate.set(Calendar.MINUTE, 0);
            nowDate.set(Calendar.SECOND, 0);
            nowDate.set(Calendar.MILLISECOND, 0);
            sendParam.selectDate = nowDate.getTime();
            sendParam.startTime = TimeComparisonUtils.hourAdd(sendParam.selectDate, 9);
            sendParam.endTime = calendarAddHour(sendParam.startTime, 2);
        }

        return sendParam;
    }

    public static Date getNowTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获取当前月份
     * @return
     */
    public static String getM(){
        SimpleDateFormat formatter = new SimpleDateFormat("M");
        String dateString = formatter.format(new Date());
        return dateString;
    }

    public static Date getEndDateTime(Date startdate, String enddate){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String endDateTime = dateFormat.format(startdate) + " " + enddate;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            return df.parse(endDateTime);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return null;
    }

    public static boolean isHoursAndMinutesEqual(Date oldDate, Date newDate){
        if(dateTTime(oldDate).equals(dateTTime(newDate))){
            return true;
        }
        return false;
    }

    /**
     * 获取某日期区间的所有日期  日期倒序
     *
     * @param startDate  开始日期
     * @param endDate    结束日期
     * @param dateFormat 日期格式
     * @return 区间内所有日期
     */
    public static List<String> getPerDaysByStartAndEndDate(String startDate, String endDate, String dateFormat) {
        DateFormat format = new SimpleDateFormat(dateFormat);
        try {
            Date sDate = format.parse(startDate);
            Date eDate = format.parse(endDate);
            long start = sDate.getTime();
            long end = eDate.getTime();
            if (start > end) {
                return null;
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(eDate);
            List<String> res = new ArrayList<String>();
            while (end >= start) {
                res.add(format.format(calendar.getTime()));
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                end = calendar.getTimeInMillis();
            }
            return res;
        } catch (ParseException e) {
        }

        return null;
    }

    public static long getDatePoor(Date nowDate, Date endDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 30;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少小时
        long hour = diff / nh;
        return hour;
    }

    /**
     * 作废
     * @param date
     * @param addHourSize
     */
    public static Date calendarAddHour(Date date, int addHourSize){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, addHourSize);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date);
        calendar2.add(Calendar.DATE, 1);
        calendar2.set(Calendar.HOUR_OF_DAY, 0);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.SECOND, 0);
        calendar2.set(Calendar.MILLISECOND, 0);
        if(calendar.getTime().getTime() >= calendar2.getTime().getTime()){
            calendar.add(Calendar.DATE, -1);
            calendar.add(Calendar.HOUR_OF_DAY, 22);
            calendar.set(Calendar.MINUTE, 30);
        }
        return calendar.getTime();
    }

    /**
     * 时间增加几小时
     * @param date
     * @param addHourSize
     */
    public static Date calendarAddHour2(Date date, int addHourSize){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, addHourSize);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

}
